<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="活动banner" prop="activityBanner">-->
<!--        <el-input-->
<!--          v-model="queryParams.activityBanner"-->
<!--          placeholder="请输入活动banner"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="开始时间" prop="startTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.startTime"-->
<!--          type="date"-->
<!--          value-format="YYYY-MM-DD"-->
<!--          placeholder="请选择开始时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="结束时间" prop="endTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.endTime"-->
<!--          type="date"-->
<!--          value-format="YYYY-MM-DD"-->
<!--          placeholder="请选择结束时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="banner跳转链接" prop="link">-->
<!--        <el-input-->
<!--          v-model="queryParams.link"-->
<!--          placeholder="请输入banner跳转链接"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['business:activityCenter:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:activityCenter:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:activityCenter:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['business:activityCenter:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="activityCenterList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="" align="center" prop="id" v-if="true"/>-->
      <el-table-column label="活动banner" align="center" prop="activityBanner">
        <template #default="scope">
          <ImagePreview
            :width="100" :height="100"
            :src="scope.row.activityBanner"
            :preview-src-list="[scope.row.activityBanner]"/>
        </template>
      </el-table-column>
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="活动类型" align="center" prop="activityType">
        <template #default="scope">
          <dict-tag :options="sys_activity_type" :value="scope.row.activityType" />
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
      </el-table-column>
      <el-table-column label="跳转类型" align="center" prop="linkType">
        <template #default="scope">
          <dict-tag :options="banner_link_type" :value="scope.row.linkType" />
        </template>
      </el-table-column>
      <el-table-column label="banner跳转链接" align="center" show-overflow-tooltip prop="link">
      </el-table-column>
      <el-table-column label="活动状态" align="center" prop="activityStatus">
        <template #default="scope">
          <dict-tag :options="sys_activity_status" :value="scope.row.activityStatus" />
        </template>
      </el-table-column>
      <el-table-column label="显示顺序" align="center" prop="orderNum" />
      <el-table-column label="显示状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="sys_show_hide" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remake" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['business:activityCenter:edit']">修改</el-button>
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['business:activityCenter:remove']">删除</el-button>
          </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改活动中心配置对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="activityCenterRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="活动banner" prop="activityBanner">
          <image-uploads v-model="form.activityBanner" :limit="1"></image-uploads>
        </el-form-item>
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="活动类型" prop="activityType">
          <el-select v-model="form.activityType" placeholder="请选择">
            <el-option
              v-for="dict in sys_activity_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="跳转类型" prop="linkTpe">
          <el-select v-model="form.linkType" placeholder="请选择" @change="linkTpeChanged">
            <el-option
              v-for="dict in banner_link_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="跳转地址" prop="link">
          <el-input
            v-if="bannerLinkType === '1' || bannerLinkType === undefined "
            v-model="form.link"
            placeholder="请输入跳转内链"
          />

          <el-input
            v-if="bannerLinkType === '2' "
            v-model="form.link"
            placeholder="请输入跳转外链"
          />
          <image-uploads
            v-if="bannerLinkType === '3'"
            v-model="form.link"
            limit="1">

          </image-uploads>

          <el-input
            v-if="bannerLinkType === '4' "
            v-model="form.link"
            placeholder="请输入文本内容"
          />
        </el-form-item>
        <el-form-item label="显示排序" prop="orderNum">
          <el-input-number v-model="form.orderNum" controls-position="right" :min="0" />
        </el-form-item>
        <el-form-item label="显示状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in sys_show_hide"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remake">
          <el-input type="textarea" :rows="4" v-model="form.remake" placeholder="备注"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ActivityCenter">
import { listActivityCenter, getActivityCenter, delActivityCenter, addActivityCenter, updateActivityCenter } from "@/api/business/activityCenter";
import ImageUploads from "@/components/ImageUploads/index.vue";

const { proxy } = getCurrentInstance();
const { matter_type,banner_link_type,sys_show_hide ,sys_activity_status,sys_activity_type} = proxy.useDict("matter_type","banner_link_type","sys_show_hide","sys_activity_status","sys_activity_type");
const activityCenterList = ref([]);
//跳转类型判断
const bannerLinkType = ref("1");
const open = ref(false);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    activityBanner: undefined,
    title: undefined,
    startTime: undefined,
    endTime: undefined,
    link: undefined,
    linkType: undefined,
    status: undefined,
    activityStatus: undefined,
  },
  rules: {
    id: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    activityBanner: [
      { required: true, message: "活动banner不能为空", trigger: "blur" }
    ],
    title: [
      { required: true, message: "标题不能为空", trigger: "blur" }
    ],
    startTime: [
      { required: true, message: "开始时间不能为空", trigger: "blur" }
    ],
    endTime: [
      { required: true, message: "结束时间不能为空", trigger: "blur" }
    ],
    link: [
      { required: true, message: "banner跳转链接不能为空", trigger: "blur" }
    ],
    linkType: [
      { required: true, message: "跳转链接的类型不能为空", trigger: "change" }
    ],
    status: [
      { required: true, message: "显示状态不能为空", trigger: "change" }
    ],
    activityStatus: [
      { required: true, message: "活动状态 0 = 未开始，1 = 进行中，2 = 已结束不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询活动中心配置列表 */
function getList() {
  loading.value = true;
  listActivityCenter(queryParams.value).then(response => {
    activityCenterList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}
/* 动态显隐跳转链接类型 */
function linkTpeChanged(e){
  bannerLinkType.value = e
}
// 表单重置
function reset() {
  form.value = {
    id: null,
    activityBanner: null,
    title: null,
    startTime: null,
    endTime: null,
    link: null,
    linkType: null,
    orderNum: 0,
    status: '0',
    activityStatus: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    delFlag: null
  };
  proxy.resetForm("activityCenterRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加活动中心配置";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  loading.value = true
  reset();
  const _id = row.id || ids.value
  getActivityCenter(_id).then(response => {
    loading.value = false;
    form.value = response.data;
    open.value = true;
    title.value = "修改活动中心配置";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["activityCenterRef"].validate(valid => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id != null) {
        updateActivityCenter(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      } else {
        addActivityCenter(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        }).finally(() => {
          buttonLoading.value = false;
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除活动中心配置编号为"' + _ids + '"的数据项？').then(function() {
    loading.value = true;
    return delActivityCenter(_ids);
  }).then(() => {
    loading.value = true;
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  }).finally(() => {
    loading.value = false;
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('business/activityCenter/export', {
    ...queryParams.value
  }, `activityCenter_${new Date().getTime()}.xlsx`)
}

getList();
</script>
